extends RigidBody2D

enum Status{
	alive,died,kick
}

var speed_up := 1.0
@export var SPEED = 50
@export var direction = 1 ## 移动方向 1=向右,2=向左
@export var init_move = false ## 初始化时是否是移动状态
@export var start_move_point: Area2D # 当player进入此区域时，触发该怪物移动
var objStatus := Status.alive
var objName := "KoopaTroopa"
# 足球属性
var kick_force = 20
var attack_player = false

@onready var animated_sprite = $AnimatedSprite2D
@onready var ray_cast_left = $RayCastLeft
@onready var ray_cast_right = $RayCastRight
@onready var damage_box = $DamageBox
@onready var hit_box = $HitBox
@onready var collision_shape_2d = $CollisionShape2D


# Called when the node enters the scene tree for the first time.
func _ready():
	hit_box.body_entered.connect(_on_hit_box_body_entered)
	damage_box.body_entered.connect(_on_damage_box_body_entered)
	if !init_move :
		start_move_point.body_entered.connect(_on_start_move)
	pass # Replace with function body.


func _physics_process(delta):
	var needChangeDirection = true
	
	if objStatus == Status.kick:
		needChangeDirection = detect_enemy()
	if objStatus == Status.died:
		kick_detect()
		return
	if !init_move:
		return
	
	if needChangeDirection:
		change_direction()
	var movement = Vector2(direction * SPEED * speed_up, 0) * delta
	move_and_collide(movement)
	
func change_direction():
	if ray_cast_right.is_colliding(): 
		direction = -1 
		animated_sprite.flip_h = false
	if ray_cast_left.is_colliding():
		direction = 1
		animated_sprite.flip_h = true
	

func detect_enemy():
	if ray_cast_right.is_colliding(): 
		return emit_hit_event(ray_cast_right)
	if ray_cast_left.is_colliding(): 
		return emit_hit_event(ray_cast_left)
	return true

func emit_hit_event(ray_cast):
	var target = ray_cast.get_collider()
	if target == null :
		return false
	if attack_player && target.name == "Player":
		print("%s 击中了 %s" % [objName,target.name])
		target.take_hit(self,1)
		return false
	print("%s 击中了 %s" % [objName,target.name])
	if target.has_method("_hit_box"):
		target._hit_box(self)
		return false
	return true

func kick_detect():
	if ray_cast_right.is_colliding(): 
		kick(Vector2(-1,0), 1.0)
	if ray_cast_left.is_colliding():
		kick(Vector2(1,0), 1.0)
		

func _on_start_move(_body):
	print("%s 开始移动" % objName)
	init_move = true

func _on_hit_box_body_entered(_body):
	print("%s 受到来自Player的1伤害" % objName)
	_died()
	
func _hit_box(_body):
	print("%s 受到来自 %s 的1伤害" % [objName,_body.name])
	_died()
	
func _died():
	objStatus = Status.died
	animated_sprite.play("died")
	damage_box.visible = false
	hit_box.visible = false
	await get_tree().create_timer(0.1).timeout


func _on_damage_box_body_entered(body):
	if body == self:
		return
	print("%s 给[%s]造成一次伤害" % [objName,body.name])
	if objStatus == Status.kick:
		pass
	if objStatus == Status.died:
		return
	#print("%s 给Player造成一次伤害" % objName)
	body.call("take_hit",self,1)

func kick(d: Vector2, force_multiplier: float = 1.0):
	# 应用踢球力
	speed_up = 4
	apply_impulse(Vector2.ZERO, d.normalized() * kick_force * force_multiplier)
	objStatus = Status.kick
	$AudioStreamPlayer.play()
	$Timer.start()

func _on_timer_timeout() -> void:
	attack_player = true
